package io.geekidea.springbootplus.system.controller;

import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.entity.SysComment;
import io.geekidea.springbootplus.system.entity.SysOrder;
import io.geekidea.springbootplus.system.entity.SysPermission;
import io.geekidea.springbootplus.system.service.SysCommentService;
import io.geekidea.springbootplus.system.vo.SysCommentQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/SysComment")
@Module("system")
@Api(value = "查看商品评价", tags = {"商品评价"})
public class SysCommentController {

    @Autowired
    private SysCommentService sysCommentService;

    @GetMapping("/getSysCommentList/{productId}")
    @RequiresPermissions("sys:permission:all:menu:list")
    @OperationLog(name = "获取订单评价", type = OperationLogType.LIST)
    @ApiOperation(value = "获取订单评价", response = SysPermission.class)
    public ApiResult<List<SysCommentQueryVo>> getSysCommentList(@PathVariable("productId") Long productId) throws Exception {
        List<SysCommentQueryVo> list =  sysCommentService.getSysCommentByProductId(productId);
        return ApiResult.ok(list);
    }

    @PostMapping( "/addSysComment")
    @OperationLog(name = "填写评论", type = OperationLogType.ADD)
    @ApiOperation(value ="填写评论",response = ApiResult. class)
    public ApiResult<Boolean> addSysComment(@Validated(Add.class) @RequestBody SysComment sysComment) throws Exception {
        boolean flag = sysCommentService.addSysComment(sysComment);
        return ApiResult.result(flag);
    }

    @PostMapping("/delete/{orderId}")
    @OperationLog(name = "删除评论", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除评论", response = ApiResult.class)
    public ApiResult<Boolean> deleteSysCommentByOrderId(@PathVariable("orderId") Integer orderId) throws Exception {
        boolean flag = sysCommentService.deleteSysCommentByOrderId(orderId);
        return ApiResult.result(flag);
    }

}
